﻿#include "CameraWorker.h"
#include  <QDebug>
CameraWorker::CameraWorker(QObject *parent) : QObject(parent)
{


}

void CameraWorker::setCameraHandle(TUCAM_OPEN cameraHandle, TUCAM_FRAME frame)
{
    m_opCam = cameraHandle;
    m_frame = frame;
    m_running= true;

  qDebug() << "success to set.";
}

void CameraWorker::processCameraFrames()
{

    while(m_running) {

        if (TUCAM_Buf_WaitForFrame(m_opCam.hIdxTUCam, &m_frame) == TUCAMRET_SUCCESS) {

            if (m_frame.pBuffer != nullptr) {
                emit frameReady(m_frame); // 发射信号，将帧传递给主线程
                //qDebug() << "success to capture frame.";

            }
        } else {
            qDebug() << "Failed to capture frame.";
        }


        QCoreApplication::processEvents();//添加处理线程循环外的任务（处理线程停止）
    }

}

void CameraWorker::stop()
{

    m_running=false;
    qDebug() << "Stopping worker in thread:" << QThread::currentThread();
    TUCAM_Buf_AbortWait(m_opCam.hIdxTUCam);
    TUCAM_Cap_Stop(m_opCam.hIdxTUCam);

    qDebug() << "Stopping worker in thread:";
}

void CameraWorker::ThreadStart()
{
     // timer->start(10);

      m_running=true;
      processCameraFrames();

}

